﻿using SarsClient;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;


namespace SarsClient
{

	public partial class MainForm : Form
	{
		
		// Name for a html template file to display in WebBrowser control.
		// Should contain some nice CSS style definitions :D
		const string HTML_TEMPLATE_NAME = "test.html";
		
		// Action run to send message
		public DelSendMessage OnSendMessage;// = delegate(string s){};
		// Action run to exit application
		public DelExitApp OnExitApp;
		
		/// <summary>
		/// Send message. This invokes the registered
		/// delegate OnSendMessage.
		/// </summary>
		/// <param name="message"></param>
		/// 
		
		
		
		void SendMessage(string message){
			
			// Send message
			OnSendMessage.Invoke(0xFFFF,message);
		}
		
		// Register own action implementations on controller
		public void SubscribeOnController(MainController ctrlr){
			ctrlr.OnDisplayMessage += new DelDisplayMessage(this.DisplayMessage);


           
		}
		
		/// <summary>
		/// Display message in chat window.
		/// </summary>
		/// <param name="sender">Sender ID.</param>
		/// <param name="time">Timestamp of received message.</param>
		/// <param name="code">Constant to use special text formatting options (e.g. blue color for own, red color for other text in chat window).</param>
		/// <param name="message">Message text.</param>
		void DisplayMessage(UInt16 sender, DateTime time, uint code, string message){
			
			// Invoke required on cross thread call into GUI
			if (this.InvokeRequired){
				this.Invoke(new MethodInvoker(delegate(){
				  DisplayMessage(sender,time,code,message);
				}));
				return ;
			} else {
				
				// Append html element
				string css_box_classname = "test1";
				
				if (code == 0)
					css_box_classname = "test1";
				string append = String.Format("{0} ({1:T}): <div class=\"{2}\">{3}</div><br/>",sender,time,css_box_classname,message);
				//MessageBox.Show(append);
		
				AddFormattedText(append.ToString());
				try {
				System.Media.SoundPlayer sound = new System.Media.SoundPlayer("alert.wav");
				sound.Play();
				} catch { }
			}
		}
		
		/// <summary>
		/// Appends HTML text to webbrowser control.
		/// </summary>
		/// <param name="innerHtml">HTML code.</param>
		void AddFormattedText(string innerHtml){
			if (this.webBrowser.Visible)
				this.webBrowser.Document.Body.InnerHtml += innerHtml;
			else
				this.richTextBox1.AppendText(innerHtml+Environment.NewLine);


            this.webBrowser.Document.Body.ScrollTop = Int32.MaxValue;
			
		}
		
		/// <summary>
		/// Initialize WebBrowser control.
		/// </summary>
		public void InitWebBrowser(){
			this.webBrowser.Visible = System.Environment.OSVersion.Platform.ToString().Contains("Win");
			this.richTextBox1.Visible = !this.webBrowser.Visible;
			
			
			// Load external resource file
			string resFile = Application.StartupPath+@"\"+HTML_TEMPLATE_NAME;
			resFile = "file://"+resFile.Replace(@"\","/");
			this.Text = resFile;
			
			
			
			this.webBrowser.Navigate(resFile);
			/*if (System.IO.File.Exists(resFile))
				this.webBrowser.Navigate(resFile);
			else
				this.webBrowser.Navigate("about:blank");*/
			this.webBrowser.AllowNavigation = false;
			
		}
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			InitWebBrowser();
			//EventSendMessage = delegate(string message){ this.DisplayMessage("local",DateTime.Now,0,message); };
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void BtnSendClick(object sender, EventArgs e)
		{
			this.SendMessage(this.tbMessage.Text);
		}
		
		void MainFormFormClosed(object sender, FormClosedEventArgs e)
		{
			OnExitApp.Invoke();
		}
		
		void TbMessageKeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == (char)13) // if enter key (#13) pressed
				DoSend();
		}
		
		void DoSend(){
			this.OnSendMessage.Invoke(0xFFFF,this.tbMessage.Text);
			this.tbMessage.Text = "";
			
		}
		
		void W7Button1ButtonClicked(object sender, EventArgs e)
		{
			DoSend();
		}
	}
}
